package com.tech.view.dialog;

import com.tech.repository.dto.GoodsInfo;
import com.tech.repository.entity.Order;
import com.tech.service.OrderService;
import com.tech.service.impl.ServiceImplFactory;
import com.tech.util.LogProcessUtil;
import com.tech.util.TestDataUtil;
import com.tech.view.common.FontUtil;
import com.tech.view.common.GraphicsUtil;
import com.tech.view.component.button.SimpleIconButton;
import com.tech.view.panel.GoodsItemPanel;
import org.jdesktop.swingx.VerticalLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.logging.Logger;

/**
 * 商品购买对话框
 */
public class GoodsPurchaseDialog extends JPanel {
	private final static Logger LOG = LogProcessUtil.getLogger(GoodsPurchaseDialog.class.getName());

	private OrderService orderService;

	private JDialog dialog;
	private JFrame frame;
	private GoodsInfo goodsInfo;
	private Order order;

	private JPanel topPanel;
	private JPanel contentPanel;
	private JPanel bottomPanel;

	private JLabel titleLabel;
	private GoodsItemPanel goodsItemPanel;
	private SimpleIconButton addressButton;
	private JButton cancelButton;
	private JButton purchaseButton;

	private int width;
	private int height;

	public GoodsPurchaseDialog(JFrame frame, Order order, GoodsInfo goodsInfo, int width, int height) {
		this.frame = frame;
		this.order = order;
		this.goodsInfo = goodsInfo;
		this.width = width;
		this.height = height;
		initService();
		initComponents();
		setListeners();
		initView();
	}

	private void initService() {
		orderService = ServiceImplFactory.getOrderService();
	}

	private void initComponents() {
		topPanel = new JPanel(new BorderLayout());
		contentPanel = new JPanel(new VerticalLayout());
		bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));

		titleLabel = new JLabel("购买书籍", SwingConstants.CENTER);
		titleLabel.setFont(FontUtil.TITLE_FONT.deriveFont(25f));

		goodsItemPanel = new GoodsItemPanel(goodsInfo, 530, 100);

		BufferedImage addressImage = GraphicsUtil.createScaledCompositeInstance(TestDataUtil.ADDRESS_PAINTER_IMAGE, width, 100);
		addressButton = new SimpleIconButton(addressImage);


		cancelButton = new JButton("取消");
		purchaseButton = new JButton("购买");
	}

	private void initView() {
		setLayout(new VerticalLayout());
		setPreferredSize(new Dimension(width, height));

		topPanel.add(titleLabel);
		topPanel.add(cancelButton, BorderLayout.EAST);

		contentPanel.add(goodsItemPanel);
		contentPanel.add(addressButton);

		bottomPanel.add(cancelButton);
		bottomPanel.add(purchaseButton);

		add(topPanel);
		add(contentPanel);
		add(bottomPanel);

		dialog = new JDialog(frame, true);
		dialog.add(this);
		dialog.pack();
		dialog.setLocationRelativeTo(null);
		dialog.setVisible(true);
	}

	private void setListeners() {
		purchaseButton.addActionListener(e -> {
			int choice = JOptionPane.showConfirmDialog(frame,
					String.format("请支付%.2f\n[支付二维码]", goodsInfo.getGoods().getGsecprice()),
					"支付对话框", JOptionPane.YES_NO_OPTION);
			if (choice == JOptionPane.OK_OPTION) {
				// 修改订单状态
				if (orderService.updateOrderState(order.getBoid(), Order.OrderState.PAYMENT)) {
					JOptionPane.showConfirmDialog(frame, "支付成功", "支付对话框", JOptionPane.YES_NO_OPTION);
				} else {
					JOptionPane.showConfirmDialog(frame, "该操作未成功", "支付对话框", JOptionPane.YES_NO_OPTION);
					LOG.warning("ID为 " + order.getBoid() + " 的订单状态修改失败，具体信息为\n" + order + "\n" + goodsInfo);
				}
				// TODO 这里暂时关闭对话框，后续修改商品详情页购买按钮
				dialog.dispose();
			}
		});
		cancelButton.addActionListener(e -> {
			dialog.dispose();
		});
	}

	public void setVisible(boolean visible) {
		dialog.setVisible(visible);
	}
}
